<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use function Matrix\add;    //引入hash

class SoundEffectRepository
{
    /**
     * 新增
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addSoundEffect($param) {
        $addConfig = app('model_room_sound_effect')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 删除
     */
    public function delSoundEffect($idArr = []) {
        $res = app('model_room_sound_effect')->whereIn("id", $idArr)->delete();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveSoundEffect($where,$param) {
        $saveConfig = app('model_room_sound_effect')->where($where)->update($param);

        if ($saveConfig!==false) return true;
        throw new BasicException(93047);
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getSoundEffectQueryList(Array $params) {
        $whereArr = [];

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];
        $result = app('model_room_sound_effect')->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        $roomTypeList = app('model_room_type')->get();
        $roomTypeMap = [];

        foreach($roomTypeList as $val)
        {
            $roomTypeMap[$val->id] = $val;
        }

        foreach ($result as $item) {
            if (!empty($item['room_type'])) {
                $roomTypeIdArr = explode("," , $item['room_type']);

                $roomTypeArr = [];
                foreach ($roomTypeIdArr as $roomType) {
                    if (isset($roomTypeMap[$roomType])) {
                        $roomTypeArr[] = $roomTypeMap[$roomType]->room_type_name;
                    }
                }
                $item->room_type_name = implode(",", $roomTypeArr);
            }
        }

        return $result;
    }
}